using Services;
using UnityEngine;

public class StatusBar : MonoBehaviour
{
    private MyObject myObject;
    private Canvas canvas;
    private MyCanvasGrounp myCanvasGrounp;
    private NPC npc;
    private Transform character_transform;
    private float distance;

    private void Awake()
    {
        myObject = GetComponent<MyObject>();
        canvas = GetComponent<Canvas>();
        myCanvasGrounp = GetComponent<MyCanvasGrounp>();
        myObject.OnActivate += OnActivate;
    }

    private void OnActivate()
    {
        canvas.worldCamera = Camera.main;
        npc = GetComponentInParent<NPC>();
        character_transform = npc.transform;
        distance = Vector2.SqrMagnitude(character_transform.localScale + npc.offset_indicatorPoint);
        myCanvasGrounp.immediate_next = true;
        myCanvasGrounp.Visible = false;
    }

    private void Update()
    {
        if (npc != null && npc.VisibleDetector.Visible)
            myCanvasGrounp.Visible = Vector2.SqrMagnitude(Camera.main.ScreenToWorldPoint(Input.mousePosition) - character_transform.position) <= distance;
        else
            myCanvasGrounp.Visible = false;
    }
}